package com.lyh.common.config;

import com.alipay.api.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class AlipayClientConfig {

    @Value("${alipay.gateway-url:}")
    private String gatewayUrl;

    @Value("${alipay.app.id:}")
    private String appId;

    @Value("${alipay.merchant-private-key:}")
    private String privateKey;

    @Value("${alipay.alipay-public-key:}")
    private String publicKey;

    @Value("${alipay.alipayPublicCert:}")
    private String alipayPublicCert;

    @Value("${alipay.alipayRootCert:}")
    private String alipayRootCert;

    @Value("${alipay.appPublicCert:}")
    private String appPublicCert;

  /*  @Bean
    public AlipayClient alipayClient() throws AlipayApiException {
        AlipayConfig alipayConfig = new AlipayConfig();
        //设置网关地址
        alipayConfig.setServerUrl(gatewayUrl);
        //设置应用ID
        alipayConfig.setAppId(appId);
        //设置应用私钥
        alipayConfig.setPrivateKey(privateKey);
        //设置请求格式，固定值json
        alipayConfig.setFormat(AlipayConstants.FORMAT_JSON);
        //设置字符集
        alipayConfig.setCharset(AlipayConstants.CHARSET_UTF8);
        //设置支付宝公钥
//        alipayConfig.setAlipayPublicKey(publicKey);
        //设置签名类型
   //     alipayConfig.setSignType(AlipayConstants.SIGN_TYPE_RSA2);

        //证书方式
        alipayConfig.setAppCertPath(appPublicCert);
        alipayConfig.setAlipayPublicKey(alipayPublicCert);
        alipayConfig.setRootCertPath(alipayRootCert);
        alipayConfig.setSignType(AlipayConstants.SIGN_TYPE_RSA2);


        //构造client
        AlipayClient alipayClient = new DefaultAlipayClient(alipayConfig);

        return alipayClient;
    }*/


    @Bean
    public AlipayClient alipayClient() throws AlipayApiException {
        CertAlipayRequest alipayConfig = new CertAlipayRequest();
        //设置网关地址
        alipayConfig.setServerUrl(gatewayUrl);
        //设置应用ID
        alipayConfig.setAppId(appId);
        //设置应用私钥
        alipayConfig.setPrivateKey(privateKey);
        //设置请求格式，固定值json
        alipayConfig.setFormat(AlipayConstants.FORMAT_JSON);
        //设置字符集
        alipayConfig.setCharset(AlipayConstants.CHARSET_UTF8);


        //证书方式
        alipayConfig.setCertPath(appPublicCert);
        alipayConfig.setAlipayPublicCertPath(alipayPublicCert);
        alipayConfig.setRootCertPath(alipayRootCert);
        alipayConfig.setSignType(AlipayConstants.SIGN_TYPE_RSA2);


        //构造client
        AlipayClient alipayClient = new DefaultAlipayClient(alipayConfig);

        return alipayClient;
    }
}
